added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSWF4ServiceHostFactory / ReadMe.txt
blob7231ed499e4715af2478f228fad3813cd47bf88c
1 =============================================================================
2         WF4/WCF APPLICATION : CSWF4ServiceHostFactory
3 =============================================================================
4 /////////////////////////////////////////////////////////////////////////////
5 Use:
7 By using WorkflowServiceHost class, we can create a WF4 service host in code. 
8 The advantage of using WorkflowServiceHost is that we can add our own workflow 
9 extensions, tracking participant and persistence store.
11 So question is: can we use our own WorkflowServiceHost in IIS7 like we did in 
12 the console application? This sample is for answering this question.
14 To run the sample:
15 1. Open CSWF4ServiceHostFactory.sln with Visual Studio 2010
16 2. Press Ctrl+F5.
19 /////////////////////////////////////////////////////////////////////////////
20 Prerequisite
22 1. Visual Studio 2010
23 2. .NET Framework 4.0
24 3. Sql Server
27 /////////////////////////////////////////////////////////////////////////////
28 Creation:
30 1.Create a Workflow Service project named CSWF4ServiceHostFactory.
31 2.Add assembly references to:
32   System.Workflow.Runtime
33   System.Workflow.Activities
34   System.ServiceModel.Activation
35   System.Configuration
36   System.Activities.DurableInstancing
37   System.Runtime.DurableInstancing
38 3.Setup WF4 sql persistence store. 
39   http://msdn.microsoft.com/en-us/library/ee395773.aspx
40   name the persistence database:WF4PersistenceDB.
41 4.add the following configuration to web.config file right under <configuration> node. 
42         <appSettings>
43            <add key="SqlWF4PersistenceConnectionString" 
44                         value="Data Source=.\sqlexpress;Initial Catalog=WF4PersistenceDB;Integrated Security=True" />
45         </appSettings>
46 5.Create a code file named MyServiceHostFactory.cs
47         using System;
48         using System.Collections.Generic;
49         using System.Workflow.Activities;
50         using System.Activities;
51         using System.Workflow.Runtime;
52         using System.ServiceModel.Activities.Activation;
53         using System.ServiceModel.Activities;
54         using System.ServiceModel.Description;
55         using System.Activities.DurableInstancing;
56         using System.Configuration;
58         namespace CSWF4ServiceHostFactory
59         {
60                 public class MyServiceHostFactory : 
61                                 System.ServiceModel.Activities.Activation.WorkflowServiceHostFactory 
62                 {
63                         protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service,
64                                                                                                                                                          Uri[] baseAddresses) 
65                         {
66                                 WorkflowServiceHost host = 
67                                         base.CreateWorkflowServiceHost(service, baseAddresses);
68                                 string connectionString = 
69                                         ConfigurationManager.AppSettings["SqlWF4PersistenceConnectionString"].ToString();
70                                 SqlWorkflowInstanceStore instanceStore = 
71                                         new SqlWorkflowInstanceStore(connectionString);
72                                 instanceStore.InstanceCompletionAction = 
73                                         InstanceCompletionAction.DeleteNothing;
74                                 host.DurableInstancingOptions.InstanceStore = instanceStore;
75                                 return host;
76                         }
77                 }
78         }
80 6.Open web.config file and add a ServiceHostingEnvironment node under the 
81   System.serviceModel. 
82         <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
83                 <serviceActivations>
84                         <add relativeAddress="~/Service1.xamlx"
85                                                 service="Service1.xamlx"
86                                                 factory="CSWF4ServiceHostFactory.MyServiceHostFactory"/>
87                 </serviceActivations>
88         </serviceHostingEnvironment>
89 7.Open the default created Service1.xaml, select SendResponse activity
90   check its PersistBeforeSend property. 
91 8.Build the project and then create a IIS7 WebSite(Framework 4.0), map the physical path 
92   to the project path. 
93   Note: To prevent that the default Application Pool identity have no permission to access the 
94   Web.config file and database, you can try shift the identity to LocalSystem. 
95 9.Call Service1.xamlx. you can call it in WcfTestClient.exe.(usually, you can 
96   find WcfTestClient.exe in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE)
99 /////////////////////////////////////////////////////////////////////////////
100 Reference:
102 http://xhinker.com/post/WF4Create-Your-Own-ServiceHostFactory.aspx